package com.xgkj.diyiketang.util;



import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;


public class PalPlatform {

    private static final String TAG=PalPlatform.class.getSimpleName();
	public static boolean isOpenWIFI(Context mContext) {

		WifiManager mWifiManager = (WifiManager) mContext
				.getSystemService(Context.WIFI_SERVICE);
		if (mWifiManager.isWifiEnabled()) {
			if (mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
				
				System.out.println("-----打开");
				return true;
			} else
				return false;
		} else {
			return false;
		}
	}



	public static boolean isWap(Context context){
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNi = cm.getActiveNetworkInfo();

		if (activeNi != null && activeNi.isConnected()) {
			
			if (PalLog.DEBUG) {
				PalLog.d("PalPaltform", "Active Network Type: " + activeNi.getTypeName());
			}

			int type = activeNi.getType();
			if(type != ConnectivityManager.TYPE_WIFI
					&& type != ConnectivityManager.TYPE_WIMAX){
				return true;
			}
		}
		return false;
	}






	
	
	public static void setWifiEnable(Context context,boolean enable){
		
		WifiManager wifimanager = (WifiManager) context
		.getSystemService(Context.WIFI_SERVICE);
		wifimanager.setWifiEnabled(enable);
	}
	





	
	
	public static String getIMEI(Context mContext) {
		TelephonyManager telephonyManager = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		String imei = telephonyManager.getDeviceId();
//		String imei = "123232ad8232328bf2";
		PalLog.d(TAG, "getIMEI: " + imei);
		return imei;
		
	}

	
    public static String getIMSI(Context mContext) {
        TelephonyManager telephonyManager = 
                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String imsi = telephonyManager.getSubscriberId();
        PalLog.d(TAG, "getImsi:" + imsi);
        return imsi;
    }
	
	public static String getMAC(Context mContext){
		String macAddress = null, ip = null;
		WifiManager wifiMgr = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
		if(info != null){
			macAddress = info.getMacAddress();
			if(macAddress == null || macAddress.length() == 0){
				for(int tryTimes = 0; tryTimes < 3; tryTimes++){
					macAddress = wifiMgr.getConnectionInfo().getMacAddress();
					if(macAddress != null){
						wifiMgr.setWifiEnabled(false);
						break;
					}
					if(wifiMgr.isWifiEnabled()){
						macAddress = wifiMgr.getConnectionInfo().getMacAddress();
						wifiMgr.setWifiEnabled(false);
						break;
					}
					if(!wifiMgr.isWifiEnabled() && info.getMacAddress() == null){
						wifiMgr.setWifiEnabled(true);
					}
//					tryTimes++;
					long l = 2000l;
					try {
						Thread.sleep(l);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
//			ip = String.valueOf(info.getIpAddress());
		}
//		PalLog.e(TAG, "macAddress = " + macAddress);
		return macAddress;
	}
	
	/*//根据IP获取本地Mac
    public static String getLocalMacAddressFromIp(Context context) {
        String mac_s= "";
       try {
            byte[] mac;
            NetworkInterface ne=NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
            mac = ne.getHardwareAddress();
            mac_s = byte2hex(mac);
       } catch (Exception e) {
           e.printStackTrace();
       }
       PalLog.e(TAG, "------**************mac = " + mac_s);
        return mac_s;
    }
    
    public static String getLocalIpAddress() {  
        try {  
            String ipv4;  
            List<NetworkInterface>  nilist = Collections.list(NetworkInterface.getNetworkInterfaces());  
            for (NetworkInterface ni: nilist)   
            {  
                List<InetAddress>  ialist = Collections.list(ni.getInetAddresses());  
                for (InetAddress address: ialist){  
                    if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress()))   
                    {   
                        return ipv4;  
                    }  
                }  
   
            }  
   
        } catch (SocketException ex) {  
            PalLog.e(TAG, ex.toString());  
        }  
        return null;  
    } 
    
    public static  String byte2hex(byte[] b) {
         StringBuffer hs = new StringBuffer(b.length);
         String stmp = "";
         int len = b.length;
         for (int n = 0; n < len; n++) {
          stmp = Integer.toHexString(b[n] & 0xFF);
          if (stmp.length() == 1)
           hs = hs.append("0").append(stmp);
          else {
           hs = hs.append(stmp);
          }
         }
         return String.valueOf(hs);
        }*/
	
/*	public static String getLocalMacAddressFromBusybox(){   
        String result = "";     
        String Mac = "";
        result = callCmd("busybox ifconfig","HWaddr");
         
        //如果返回的result == null，则说明网络不可取
        if(result==null){
            return "网络出错，请检查网络";
        }
         
        //对该行数据进行解析
        //例如：eth0      Link encap:Ethernet  HWaddr 00:16:E8:3E:DF:67
        if(result.length()>0 && result.contains("HWaddr")==true){
            Mac = result.substring(result.indexOf("HWaddr")+6, result.length()-1);
            PalLog.d("test","Mac:"+Mac+" Mac.length: "+Mac.length());
             
            if(Mac.length()>1){
                Mac = Mac.replaceAll(" ", "");
                result = "";
                String[] tmp = Mac.split(":");
                for(int i = 0;i<tmp.length;++i){
                    result +=tmp[i];
                }
            }
            result = Mac;
            PalLog.d("test",result+" result.length: "+result.length());            
        }
        return result;
    }   
    
    private static String callCmd(String cmd,String filter) {   
        String result = "";   
        String line = "";   
        try {
            Process proc = Runtime.getRuntime().exec(cmd);
            InputStreamReader is = new InputStreamReader(proc.getInputStream());   
            BufferedReader br = new BufferedReader (is);   
             
            //执行命令cmd，只取结果中含有filter的这一行
            while ((line = br.readLine ()) != null && line.contains(filter)== false) {   
                //result += line;
            	 PalLog.d("test","line: "+line);
            }
             
            result = line;
            PalLog.d("test","result: "+result);
        }   
        catch(Exception e) {   
            e.printStackTrace();   
        }   
        return result;   
    }*/

}
